#include <QtGui>

#include "gui/PlayArea.h"
#include "Agent31.h"

using namespace CardGraphics;

PlayArea::PlayArea(QWidget *parent) : QWidget(parent), selectedRegion(-1)
{
  setAcceptDrops(true);
  setMinimumSize(3*CardGraphics::CARDWIDTH, 9*CardGraphics::CARDHEIGHT/2);
//   setMaximumSize(400, 400);

}

 void PlayArea::reset()
{
  cardRegions.clear(); // Losing test pointers
  update();
}

 void PlayArea::startGame(const GameManager* game, const Player** players,
                          int human)
{
  reset();

  for (int i=0; i<NumStacks; i++)
    cardRegions << CCardRegion();

  cardRegions[DrawPile].SetCardStack(game->getDrawStack());
  cardRegions[DrawPile].SetOffset(QSize(2,2));
  cardRegions[DrawPile].AddAttributes(CardGraphics::Stack3d);
  cardRegions[DrawPile].RemoveAttributes(CardGraphics::StackFaceUp);
  cardRegions[DiscardPile].SetCardStack(game->getDiscardStack());
  cardRegions[DiscardPile].SetOffset(QSize(2,2));
  cardRegions[DiscardPile].AddAttributes(CardGraphics::Stack3d);
  cardRegions[PlayerHand].SetCardStack(players[human]->getHandStack());
  cardRegions[PlayerHand].SetOffset(QSize(handSeparation, 0));
  cardRegions[OppHand].SetCardStack(players[abs(human-1)]->getHandStack());
  cardRegions[OppHand].SetOffset(QSize(handSeparation, 0));
  // if the human player is really not an agent, hide the other player's hand
  if (!dynamic_cast<const Agent31*>(players[human]))
    cardRegions[OppHand].RemoveAttributes(CardGraphics::StackFaceUp);

  setStackPositions();

  update();
}

void PlayArea::revealHands() {
  cardRegions[OppHand].AddAttributes(CardGraphics::StackFaceUp);
  update();
}

 int PlayArea::targetRegion(const QPoint &position) const
{
  for (int i = 0; i < cardRegions.size(); ++i) {
    if (cardRegions[i].GetRect().contains(position)) {
      return i;
    }
  }
  return -1;
}

void PlayArea::mousePressEvent(QMouseEvent *event)
{
  if (event->buttons() != Qt::LeftButton) {
    event->ignore();
    return;
  }

  int found = targetRegion(event->pos());
  if (found == -1) {
    event->ignore();
    return;
  }

  int cardIndex = cardRegions[found].GetClickedIndex(event->pos());
  if (cardIndex < 0)
    event->ignore();
  else {
    event->accept();
    Query::debugOut(string("Click targeted ") + StackNameStrings[found] + ", index ",1);
    Query::debugValue(cardIndex,1);
  }

  if (found == DrawPile)
    emit drawPileClicked();
  else if (found == DiscardPile)
    emit discardPileClicked();
  else if (found == PlayerHand)
    emit playerHandClicked(cardIndex);

//   QRect targetRect = cardRegions[found].GetRect();
//   update(targetRect);

}

 void PlayArea::paintEvent(QPaintEvent *event)
{
  QPainter painter;
  painter.begin(this);
  painter.fillRect(event->rect(), Qt::darkGreen);

//   if (hoverRect.isValid()) {
//     painter.setBrush(QColor("#ffcccc"));
//     painter.setPen(Qt::NoPen);
//     painter.drawRect(hoverRect.adjusted(0, 0, -1, -1));
//   }

  for (int i = 0; i < cardRegions.size(); i++) {
    Query::debugOut("Drawing stack ", 2);
    Query::debugValue(i, 2);
    Query::debugOut("\n", 2);
    CardGraphics::CardDrawList drawList = cardRegions[i].getDrawList();
    Query::debugOut("\tStack has ", 2);
    Query::debugValue(drawList.size(), 2);
    Query::debugOut(" cards\n", 2);
    for (int j = 0; j < drawList.size(); j++) {
      Query::debugOut("\tDrawing card ", 3);
      Query::debugValue(j, 3);
      Query::debugOut("\n", 3);
      painter.drawPixmap(drawList[j].first, drawList[j].second);
    }
  }
  painter.end();
}

void PlayArea::resizeEvent(QResizeEvent *event) {

  handSeparation = 50;

  handX = (width()-(2*handSeparation+CardGraphics::CARDWIDTH))/2;
  if (handX<0)
    handX=0;
  drawX = width()/4 - CardGraphics::CARDWIDTH/2;
  if (drawX<0)
    drawX=0;
  discardX = drawX + width()/2;
  if (discardX < drawX+CardGraphics::CARDWIDTH)
    discardX = drawX+CardGraphics::CARDWIDTH;

  oppY = height()/4 - 2*CardGraphics::CARDHEIGHT/3;
  if (oppY < 0)
    oppY=0;
  pileY = oppY + height()/4;
  if (pileY < oppY+4*CardGraphics::CARDHEIGHT/3)
    pileY=oppY+4*CardGraphics::CARDHEIGHT/3;
  playerY = pileY + height()/4;
  if (playerY < pileY+4*CardGraphics::CARDHEIGHT/3)
    playerY=pileY+4*CardGraphics::CARDHEIGHT/3;

  if (cardRegions.size() == NumStacks) {
    setStackPositions();
  }
  QWidget::resizeEvent(event);
}

void PlayArea::setStackPositions() {
  cardRegions[OppHand].SetPosition(QPoint(handX, oppY));
  cardRegions[DrawPile].SetPosition(QPoint(drawX, pileY));
  cardRegions[DiscardPile].SetPosition(QPoint(discardX, pileY));
  cardRegions[PlayerHand].SetPosition(QPoint(handX, playerY));
}

